<template>
	<view class="goods-evaluate" :data-theme="themeStyle">
		<mescroll-uni ref="mescroll" @getData="getGoodsEvaluate">
			<block slot="list">
				<view class="evaluate-item" v-for="(item, index) in list" :key="index">
					<view class="evaluator">
						<view>
							<view class="evaluator-face">
								<image v-if="item.member_headimg" :src="$util.img(item.member_headimg)" @error="imageError(index)" mode="aspectFill" />
								<image v-else :src="$util.getDefaultImage().default_headimg" mode="aspectFill" />
							</view>
							<text class="evaluator-name">{{ item.member_name }}</text>
						</view>
						<text class="evaluator-time ns-text-color-gray">{{ $util.timeStampTurnTime(item.create_time) }}</text>
					</view>
					<view class="cont">{{ item.content }}</view>
					<scroll-view scroll-x="true" >
						<view class="evaluate-img" v-if="item.images">
							<view class="img-box" v-for="(img, img_index) in item.images" :key="img_index" @click="previewEvaluate(index, img_index, 'images')">
								<image :src="$util.img(img)" mode="aspectFit" />
							</view>
						</view>
					</scroll-view>
					
					<view class="time">
						<text>{{ item.sku_name }}</text>
					</view>
					
					<view v-if="item.explain_first != ''" class="time shop-reply-box">
						<view class="shop-reply">商家回复：</view>
						<view class="cont">{{ item.explain_first }}</view>
					</view>
					
					<template v-if="item.again_content != ''">
						<view class="review-evaluation ns-text-color">
							追加评价
							<!-- <text class="review-time ns-text-color-gray">{{ $util.timeStampTurnTime(item.again_time) }}</text> -->
						</view>
						<view class="cont">{{ item.again_content }}</view>
						<scroll-view scroll-x="true" >
							<view class="evaluate-img" v-if="item.again_images.length > 0">
								<view
									class="img-box"
									v-for="(again_img, again_index) in item.again_images"
									:key="again_index"
									@click="previewEvaluate(index, again_index, 'again_images')"
								>
									<image :src="$util.img(again_img)" mode="aspectFit" />
								</view>
							</view>
						</scroll-view>
						
						<view  v-if="item.again_explain != ''" class="time shop-reply-box">
							<view class="shop-reply" v-if="item.again_explain != ''">商家回复：</view>
							<view class="cont">{{ item.again_explain }}</view>
						</view>
						
					</template>
				</view>
				<view v-if="list.length == 0"><ns-empty></ns-empty></view>
			</block>
		</mescroll-uni>
		<loading-cover ref="loadingCover"></loading-cover>
		<ns-show-toast></ns-show-toast>
	</view>
</template>

<script>
	import nsShowToast from '@/components/ns-show-toast/ns-show-toast.vue'
	import globalConfig from '@/common/js/golbalConfig.js'
export default {
	data() {
		return {
			goodsId: 0,
			list: []
		};
	},
	components: {
		nsShowToast
	},
	mixins: [globalConfig],
	onLoad(data) {
		this.goodsId = data.goods_id || 0;
	},
	onShow() {
		// 刷新多语言
		this.$langConfig.refresh();
	},
	methods: {
		getGoodsEvaluate(mescroll) {
			this.$api.sendRequest({
				url: '/api/goodsevaluate/page',
				data: {
					page: mescroll.num,
					page_size: mescroll.size,
					goods_id: this.goodsId
				},
				success: res => {
					let newArr = [];
					let msg = res.message;
					if (res.code == 0 && res.data) {
						newArr = res.data.list;
					} else {
						this.$util.showToast({ title: msg });
					}

					for (var i = 0; i < newArr.length; i++) {
						if (newArr[i].images) newArr[i].images = newArr[i].images.split(',');
						if (newArr[i].again_images) newArr[i].again_images = newArr[i].again_images.split(',');
						if (newArr[i].is_anonymous == 1)
							newArr[i].member_name = newArr[i].member_name.replace(newArr[i].member_name.substring(1, newArr[i].member_name.length - 1), '***');
					}
					mescroll.endSuccess(newArr.length);
					//设置列表数据
					if (mescroll.num == 1) this.list = []; //如果是第一页需手动制空列表
					this.list = this.list.concat(newArr); //追加新数据
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				},
				fail: res => {
					mescroll.endErr();
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			});
		},

		// 预览评价图片
		previewEvaluate(index, img_index, field) {
			var paths = [];
			for (let i = 0; i < this.list[index][field].length; i++) {
				paths.push(this.$util.img(this.list[index][field][i]));
			}
			uni.previewImage({
				current: img_index,
				urls: paths
			});
		},
		imageError(index) {
			this.list[index].member_headimg = this.$util.getDefaultImage().default_headimg;
			this.$forceUpdate();
		}
	}
};
</script>

<style lang="scss">
.goods-evaluate {
	.evaluate-item {
		margin: $ns-margin;
		padding: 20rpx;
		background: #fff;
		border-radius: 10rpx;
		.evaluator {
			display: flex;
			justify-content: space-between;
			&>view:first-of-type{
				flex-wrap: nowrap;
				flex-shrink: 0;
				display: flex;
			}
			.evaluator-face {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.evaluator-name {
				margin-left: 20rpx;
				color: #000;
				font-size: $ns-font-size-base;
				margin-top: 6rpx;
				font-weight: bold;
			}
			.evaluator-time{
				font-size: $ns-font-size-sm;
				margin-top: 8rpx;
			}
		}

		.cont {
			text-align: justify;
			display: -webkit-box;
			word-break: break-all;
			font-size: $ns-font-size-base;
			margin: 16rpx 0;
		}

		.evaluate-img {
			display: inline-flex;
			width: 100%;

			.img-box {
				flex-shrink: 0;
				width: 169rpx;
				height: 169rpx;
				overflow: hidden;
				margin: 0 20rpx 20rpx 0;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.time {
			color: #7F7F7F;
			font-size: $ns-font-size-x-sm;
			background:rgba(247,247,247,1);
			padding: 4rpx 16rpx;
			margin-bottom: $ns-margin;
			border-radius: 6rpx;
			text {
				margin-right: 20rpx;
			}
		}

		.evaluation-reply {
			margin-top: 10rpx;
			font-size: $ns-font-size-base;
		}

		.review-evaluation {
			margin-top: 20rpx;
			font-size: $ns-font-size-base;
			.review-time {
				overflow: hidden;
				float: right;
			}
		}
	}
}
.shop-reply{
	font-size: $ns-font-size-sm;
	color: #000;
	font-weight: bold;
}
.shop-reply-box{
	padding: 20rpx !important;
}
</style>
